<template>
  <el-dialog
    title="出车明细"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.sendStation" placeholder="发出站" @input="sendStationName()" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          :editable="false"
          type="date"
          :picker-options="{disabledDate}"
          v-model="dataForm.startDate"
          value-format="yyyy-MM-dd"
          style="width:150px;"
          placeholder="运输起始日期">
        </el-date-picker>
        ~
        <el-date-picker
          :editable="false"
          :picker-options="{disabledDate}"
          type="date"
          v-model="dataForm.endDate"
          style="width:150px;"
          value-format="yyyy-MM-dd"
          placeholder="运输结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>

        <el-button v-if="isAuth('tms:settlementrecord:export')" type="primary" @click="exportHandle()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table

      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        width="150px"
        prop="shipmentDate"
        header-align="center"
        align="center"
        label="运输日期">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        width="150px"-->
      <!--        prop="companyName"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="收货单位">-->
      <!--      </el-table-column>-->

      <el-table-column
        width="120px"
        prop="sendStation"
        header-align="center"
        align="center"
        label="发出站">
      </el-table-column>
      <el-table-column
        prop="receiveWeight"
        header-align="center"
        align="center"
        label="实收吨数">
      </el-table-column>
      <el-table-column
        prop="sendWeight"
        header-align="center"
        align="center"
        label="矿发吨数">
      </el-table-column>
      <el-table-column
        width="120px"
        prop="price"
        header-align="center"
        align="center"
        label="每吨运费">
      </el-table-column>
      <el-table-column
        width="120px"
        prop="shipmentAmount"
        header-align="center"
        align="center"
        label="总运费">
      </el-table-column>

      <el-table-column
        prop="shipmentCount"
        header-align="center"
        align="center"
        label="运输车次">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="salary"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="工资(元)">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column-->
      <!--        width="130px"-->
      <!--        prop="fuelAmount"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="燃料费(油/电)">-->
      <!--      </el-table-column>-->

      <!--      <el-table-column-->
      <!--        width="130px"-->
      <!--        prop="otherAmount"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="其他费用(元)">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column-->
      <!--        width="130px"-->
      <!--        prop="totalAmount"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="总支出费(元)">-->
      <!--      </el-table-column>-->
      <!--      <el-table-column-->
      <!--        prop="settlementStatus"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="结算状态">-->
      <!--        <template slot-scope="scope">-->
      <!--          {{scope.row.settlementStatus == 0?'未结算':'已结算'}}-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        width="100px"
        prop="licensePlate"
        header-align="center"
        align="center"
        label="车牌号">
      </el-table-column>
      <el-table-column
        width="100px"
        prop="driverName"
        header-align="center"
        align="center"
        label="驾驶员">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        width="170px"-->
      <!--        prop="createTime"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="创建时间">-->
      <!--      </el-table-column>-->

    </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
  </el-dialog>
</template>

<script>
import Utils from '@/utils/utils'

export default {
  data () {
    return {
      pickerOptions: {

      },

      dataForm: {
        startDate: '',
        endDate: '',
        sendStation: ''
      },
      id: null,
      visible: false,
      dataList: [],
      vehicelCodeList: [],
      driverList: [],
      receiveCompanyList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      detailVisible: false,
      importVisible: false,
      year:'',
      month:'',
      cloneList:[]
    }
  },

  methods: {
    sendStationName(){
      this.pageIndex =1;
      this.getDataList();
      // let list = []
      // list = this.dataList.filter(item => {
      //   return item.sendStation.toLowerCase().indexOf(this.dataForm.sendStation.toLowerCase()) !== -1
      //   // return item.name.toLowerCase().includes(this.name.toLowerCase());
      // })
      // this.dataList = list
      // if (this.dataForm.sendStation === '') {
      //   this.dataList = this.cloneList
      // }
    },
    disabledDate(time) {
      // 设置可选范围为当月第一天到最后一天
      //  let currentMonth = new Date().getMonth();
      let firstDayOfMonth = new Date(this.year, this.month-1, 0);
      let lastDayOfMonth = new Date(this.year, this.month, 0);
      return time < firstDayOfMonth || time > lastDayOfMonth;
    },
    exportHandle () {
      this.$http({
        url: this.$http.adornUrl('/tms/settlementrecord/exportDetail'),
        method: 'get',
        responseType: 'blob', //application/octet-stream
        params: this.$http.adornParams({
          'id': this.id,
          sendStation:this.dataForm.sendStation,
          startDate:this.dataForm.startDate,
          endDate:this.dataForm.endDate
        })
      }).then(({data}) => {
        if (data.size > 0) {
          Utils.download(data, this.dataForm.sendStation + '账单明细')
        }

      })
    },
    init (id,month) {
      this.month = month
      this.dataForm.startDate =month+'-01'
      this.year = month.split('-')[0]
      this.month = Number(month.split('-')[1])

      console.log(this.year, Number(this.month))
      this.id = id || 0
      this.visible = true

      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/tms/settlementrecord/detail'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          id: this.id,
          sendStation:this.dataForm.sendStation,
          startDate:this.dataForm.startDate,
          endDate:this.dataForm.endDate
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.cloneList = this.dataList
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    }

  }
}
</script>
